<?php

require_once(WSB_MODULES_PATH . 'sherpa/models/ProductGateway.php');
require_once(WSB_MODULES_PATH . 'sherpa/models/CategoryGateway.php');

class Sherpa_ProductDetailsController extends WSB_Xtension_Extension
{

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	public function init()
	{
		// Test auf fehlende Konfigurationseinstellungen
		$entries = array(
			'pageIdCategories',
			'pageIdDetails',
			'pageIdShop',
			'pageIdCart',
			'pageIdCheckoutLogin',
			'pageIdCheckout',
			'pageIdAGB',
			'pageIdLogin',
			'pageIdForgotPassword',
			'pageIdRegistration',
			'pageIdError',
			'currencyCode',
			'recipientEmail',
			'recipientName',
			'senderEmail',
			'senderName'
		);
		$missingConfigs = array();
		$configuration = $this->getConfiguration();
		foreach($entries AS $entry) {
			if(!isset($configuration->$entry)) {
				$missingConfigs[] = $entry;
			}
		}
		if(!empty($missingConfigs)) {
			$this->addContent('<div style="border:1px solid red;color:red;padding:10px;margin-bottom:1em">');
			$this->addContent('Fehler! Folgende Konfigurationseinstellungen fehlen: ');
			$this->addContent(implode(', ', $missingConfigs));
			$this->addContent('<br />Bitte in der Administration unter "Website > Installierte Module > Sherpa" für alle Instanzen editieren.');
			$this->addContent('</div>');
		}
	}

	/**
	 * Index Action
	 */
	public function indexAction()
	{
		$id = $this->getParam('id', 0);
		$languageId = $this->getLanguageId();

		if(($id < 1) || ($languageId < 1)) {
			return;
			// Zur Fehlerseite weiterleiten bei falschen Parametern
			//$this->redirectToPage($this->getConfiguration()->pageIdError);
			//exit;
		}

		$gateway = new Sherpa_Model_ProductGateway();
		$product = $gateway->fetchParent($id, $languageId);
		if($product->isLoaded()) {

			// Zuletzt besuchtes Produkt in Session speichern
			$session = new Zend_Session_Namespace('Sherpa_ProductDetails');
			$session->lastVisitedProductUrl = $this->getRequest()->getRequestUri();

			$this->renderBreadcrumb($product);

			// Lade Zusatzdaten
			$product->loadParentProductIcons();
			$product->loadParentProductSizes();
			$product->loadParentProductColorApplications();
			$product->loadParentProductImages();
			$product->loadParentProductImageVariants();
			$product->loadChildren($this->getConfiguration()->currencyCode);
			$product->loadPrice($this->getConfiguration()->currencyCode);

			$this->_view->product = $product;
			$this->addContent($this->_view->render('productdetails/index.phtml'));

		} else {

			// Zur Fehlerseite weiterleiten
			$this->redirectToPage($this->getConfiguration()->pageIdError);
			exit;
		}
	}

	/**
	 * @param Sherpa_Model_Product $product
	 */
	public function renderBreadcrumb($product)
	{
		$catId = $this->getParam('subcatId', 0);
		if(empty($catId)) {
			$catId = $this->getParam('catId', 0);
		}
		$gateway = new Sherpa_Model_CategoryGateway();
		$tree = $gateway->fetchTree($this->getLanguageId());

		if(!empty($catId)) {
			$tree = $tree->getNode($catId);
			if($tree instanceof WSB_Model_SimpleTree) {
				$rootline = $tree->getRootline();
				$rootline->reverse();
				$this->_view->rootline = $rootline;
				$this->_view->product = $product;
				$this->addContent($this->_view->render('productdetails/breadcrumb.phtml'));
			}
		}
	}

}